package dryven.unittest;

import dryven.config.xml.MemoryApplicationConfiguration;
import dryven.init.DefaultFrontControllerFactory;
import dryven.init.FrontControllerFactory;
import dryven.request.FrontController;
import dryven.request.http.Request;
import dryven.request.http.Response;
import dryven.unittest.mocks.MockRequest;
import dryven.unittest.mocks.MockResponse;
import dryven.util.ArrayIterable;
import junit.framework.TestCase;

public class FrontControllerTestCase extends TestCase {
	public void testFrontControllerFactory() throws InstantiationException, IllegalAccessException {
		MemoryApplicationConfiguration cfg = new MemoryApplicationConfiguration();
		cfg.setModelPackages(new ArrayIterable<String>(new String[] {getClass().getPackage().getName()}));
		cfg.setControllerPackages(new ArrayIterable<String>(new String[] {getClass().getPackage().getName()+".testcontrollers"}));
		cfg.setFrontControllerFactoryClass(DefaultFrontControllerFactory.class.getName());
		cfg.setUserDataDirectory("/Users/bruno/");
		//cfg.addDatabaseConfiguration(new MemoryDatabaseConfiguration(), isDefault)
		
		FrontControllerFactory fac = cfg.getFrontControllerFactoryClass().newInstance();
		FrontController fc = fac.createFrontController(cfg);
		
		assertTrue(fac!=null);
		
		MockRequest req = new MockRequest("/list/");
		MockResponse resp = new MockResponse();
		
		fc.handleRequest(req, resp);
		
		req = new MockRequest("/a/route/that/cant/match/any/of/the/controllers/in/the/test/controllers/because/its/really/really/really/long/");
		resp = new MockResponse();
		
		fc.handleRequest(req, resp);
		assertTrue(resp.getStatus()==404);
		
		req = new MockRequest("/list/");
		resp = new MockResponse();
		
		fc.handleRequest(req, resp);
		assertTrue(resp.getStatus()==200);
		
	}
}
